package li.kaeppe.travel.tracker.web.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.oxm.XmlMappingException;

import li.kaeppe.travel.tracker.domain.Trip;
import li.kaeppe.travel.tracker.parser.NoTimeStampException;
import li.kaeppe.travel.tracker.parser.ParseException;
import li.kaeppe.travel.tracker.parser.WayPointParser;
import li.kaeppe.travel.tracker.service.TripService;

public class WayPointUploadServiceImpl implements WayPointUploadService {

	/* **************************************************** */
	/* CONSTANTS */
	/* **************************************************** */

	private static final Log log = LogFactory
			.getLog(WayPointUploadServiceImpl.class);

	/* **************************************************** */
	/* MEMBER FIELDS */
	/* **************************************************** */

	private TripService tripService;

	private WayPointParser parser;

	/* **************************************************** */
	/* CONSTRUCTOR */
	/* **************************************************** */

	/* **************************************************** */
	/* PUBLIC METHODS */
	/* **************************************************** */

	public void uploadWayPoints(InputStream stream, long tripId, Date startDate)
			throws XmlMappingException, ParseException, IOException,
			NoTimeStampException {
		if (log.isDebugEnabled()) {
			log.debug("uploadWayPoints --> start adding way points to trip ["
					+ tripId + "]");
		}
		
		Trip trip;
		
		try {
			trip = this.getTripService().getTripById(tripId);
			trip.addWayPoints(this.getParser().parse(stream, startDate));
			this.getTripService().storeTrip(trip);
		} catch (XmlMappingException e) {
			log.error("Can't parse XML file [XmlMappingException]", e);
			throw e;
		} catch (ParseException e) {
			log.error("Can't parse XML file [ParseException]", e);
			throw e;
		} catch (NoTimeStampException e) {
			log.info("Can't parse XML file [NoTimeStampException]");
			throw e;
		} catch (IOException e) {
			log.error("Can't parse XML file [IOException]", e);
			throw e;
		} catch (RuntimeException e) {
			log.error("Can't parse XML file [RuntimeException]", e);
			throw e;
		}
		
		if(log.isDebugEnabled()) {
			log.debug("Successfully added way point to trip [" + trip.getTitle() + "]");
		} else {
			
		}
		
	}
	
	public void uploadWayPoints(InputStream stream, long tripId)
			throws XmlMappingException, ParseException, IOException, NoTimeStampException {
		if (log.isDebugEnabled()) {
			log.debug("uploadWayPoints --> start adding way points to trip ["
					+ tripId + "]");
		}
		
		Trip trip;
		
		try {
			trip = this.getTripService().getTripById(tripId);
			trip.addWayPoints(this.getParser().parse(stream));
			this.getTripService().storeTrip(trip);
		} catch (XmlMappingException e) {
			log.error("Can't parse XML file [XmlMappingException]", e);
			throw e;
		} catch (ParseException e) {
			log.error("Can't parse XML file [ParseException]", e);
			throw e;
		} catch (NoTimeStampException e) {
			log.info("Can't parse XML file [ParseException]");
			throw e;
		} catch (IOException e) {
			log.error("Can't parse XML file [IOException]", e);
			throw e;
		} catch (RuntimeException e) {
			log.error("Can't parse XML file [RuntimeException]", e);
			throw e;
		}
		
		if(log.isDebugEnabled()) {
			log.debug("Successfully added way point to trip [" + trip.getTitle() + "]");
		}
	}

	/* **************************************************** */
	/* PROTECTED METHODS */
	/* **************************************************** */

	/* **************************************************** */
	/* PRIVATE METHODS */
	/* **************************************************** */

	/* **************************************************** */
	/* GETTERS AND SETTERS */
	/* **************************************************** */

	public WayPointParser getParser() {
		return parser;
	}

	public void setParser(WayPointParser parser) {
		this.parser = parser;
	}

	public TripService getTripService() {
		return tripService;
	}

	public void setTripService(TripService tripService) {
		this.tripService = tripService;
	}

}
